import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { AdminModule } from './admin/admin.module';
import { ApiModule } from './api/api.module';
import { SharedModule } from './shared/shared.module';
import {
  I18nModule,
  QueryResolver,
  HeaderResolver,
  CookieResolver,
  AcceptLanguageResolver,
} from 'nestjs-i18n';
import * as path from 'path';
import { I18nTestController } from './i18n/i18n-test.controller';
import { MethodOverridesMiddleware } from './shared/middlewares/method-override.middlewares';

@Module({
  imports: [
    SharedModule,
    AdminModule,
    ApiModule,
    I18nModule.forRoot({
      fallbackLanguage: 'en', //默认语言
      loaderOptions: {
        path: path.join(__dirname, 'i18n/'),
        watch: true,
      },
      resolvers: [
        //数组越靠前，优先级越高
        //获取系统语言
        new QueryResolver(['lang', 'l']), //url传递查询，字符串 /api?lang=zh   /api?l=en
        new HeaderResolver(['x-custom-lang']), //headers:{"x-custom-lang":"zh"}
        new CookieResolver(), //cookie: x-custom-lang=zh
        AcceptLanguageResolver, //headers:{'accept-language':"zh"}
      ],
    }),
  ],
  controllers: [I18nTestController],
  providers: [],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    // 应用中间件
    consumer.apply(MethodOverridesMiddleware).forRoutes('*');
  }
}
